<template>
  <view class="container">
    <view class="bg-set"></view>
    <view class="profile">
      <!-- 信息展示 -->
      <uni-card padding="0" spacing="0">
        <view class="address_1">
          <view style="display: flex;height: 100rpx;align-items: center;">
            <view class="icon icon-dingwei"></view>
            <view style="margin-left: 20rpx;">{{storeUser.profile?.addressTxt}}</view>
          </view>
          <!-- <view style="display: flex;align-items: center;">
            <text class="icon icon-xiangyoujiantou"></text>
            <input type="text" placeholder="补充" style="width: 170rpx;height: 100rpx;">
          </view> -->

        </view>
        <uni-list>
          <uni-list-item>
            <template v-slot:header>
              <view class="address">
                <view class="icon icon-yonghu-xianxing"></view>
                <view style="margin-left: 10rpx;">{{storeUser.profile?.customerName}}</view>
              </view>
            </template>
            <template v-slot:footer>
              <view class="address">
                <view class="icon icon-dianhua-copy"></view>
                <view style="margin-left: 10rpx;">{{storeUser.profile?.phoneNumber}}</view>
              </view>
            </template>
          </uni-list-item>
          <!-- <uni-list-item>
            <template v-slot:header>
              <view>
                <view>餐别</view>
                <view>送达时间</view>
              </view>
            </template>
            <template v-slot:footer>
              <view style="color:#2979ff ;" v-for="(item,index) in requestObj" :key="index">[{{ item.orderCategoryName }}]{{配送时间}}-{{配送截止时间}}</view>
            </template>
          </uni-list-item> -->
          <uni-list-item>
            <template v-slot:header>
              <view>备注</view>
            </template>
            <template v-slot:footer>
              <input type="text" placeholder="请输入口味禁忌" style="width: 230rpx;" v-model="nots" @blur="blurFn" maxlength="20">
            </template>
          </uni-list-item>
          <uni-list-item>
            <template v-slot:header>
              <view>是否配送</view>
            </template>
            <template v-slot:footer>
              <radio checked="true"/>配送
            </template>
          </uni-list-item>
        </uni-list>
      </uni-card>
      <!-- 食堂配送菜品 -->
      <view class="kk">
        <uni-card padding="0" spacing="0">
          <uni-list>
            <uni-list-item>
              <template v-slot:header>
                <view style="display: flex;">
                  <view class="mode">配送</view>
                  <view>食堂统一配送</view>
                </view>
              </template>
            </uni-list-item>
            <!-- 需要数据循环渲染 -->
       <view v-for="(item,index) in cartList" :key="index">
        <view style="margin-left: 16rpx;color: black;font-size: 35rpx;">{{item.orderCategoryName}}</view>
        <uni-list-item>
              <template v-slot:header>
                <view style="width: 100%;">
                  <view class="food" v-for="(it,index) in item.goodsInfo" :key="index">
                  <view class="left">
                    <image :src="!it.imgPath?'https://bpic.51yuansu.com/pic3/cover/02/00/33/5984a86e2f6f5_610.jpg' :('https://api.victorick.com:8222/'+ it.imgPath)" class="img" />
                    <view>
                      <view style="font-weight: 700;">{{it.foodName||it.foodPackageName}}</view>
                      <view>*{{it.num}}</view>
                    </view>
                  </view>
                  <view>￥ {{(it.num*100)/100*(it.defaultPriceStr*100)/100}}</view>
                </view>
                </view>
              </template>
            </uni-list-item>
       </view>

            <uni-list-item>
              <template v-slot:header>
                <view>共{{storeCart.cartObj.goodsNum}}件商品</view>
              </template>
            </uni-list-item>
			
			<uni-list-item title="商品小计" v-if="showing">
			  <template v-slot:footer>
			    <view>￥ {{totalPrice}}</view>
			  </template>
			</uni-list-item>
			
			<uni-list-item title="餐补扣除" v-if="showing">
			  <template v-slot:footer>
			    <view>￥ {{preferential}}</view>
			  </template>
			</uni-list-item>
			<uni-list-item title="合计">
			  <template v-slot:footer>
			    <view>￥ {{sum}}</view>
			  </template>
			</uni-list-item>
          </uni-list>
        </uni-card>
      </view>
        <button class="btn" @click="onConfirm">{{'确认无误，立即下单' }}</button>
    </view>
  </view>
</template>

<script setup>
import { useCartStore,useMemberStore } from '../../stores';
import { ref,onMounted, nextTick } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
// import {debounce} from '../../utils/tool'
const { safeAreaInsets } = uni.getSystemInfoSync()
const cartList=ref([])
const orderList=ref()
const storeUser=useMemberStore()
const storeCart=useCartStore()
const notes=ref()//备注
const requestObj=ref()
const loading=ref(false)
let sum=ref(0)//下单总额
let showing=ref(true)
let way = ref(true)
let totalPrice = ref(0)
let preferential = ref(0)
let endTime=ref('')//营业时间
const sumFn=()=>{
  const storeCart=useCartStore()
  
  storeCart.cartObj.paymsg.forEach((item)=>{
	  if(item.payWayCode =='WX' && item.paymentAmount>0){
		  showing.value = false
		  way.value = false
		  sum.value +=  parseFloat(item.paymentAmount*100/100)
		  // sum.value += item.paymentAmount.toFixed(2)
	  } else if(item.payWayCode=='YE' && item.paymentAmount>0){
		
		  sum.value += parseFloat(item.paymentAmount*100/100)
		  // sum.value += item.paymentAmount.toFixed(2)
	  } else if(item.payWayCode=='CB' && item.paymentAmount>0){
      showing.value = true
		  preferential.value = item.paymentAmount
	  }
	   // parseFloat(item.paymentAmount*100/100) 
		totalPrice.value += parseFloat(item.paymentAmount*100/100) 
  })
  sum.value = sum.value.toFixed(2)
  console.log(sum.value,'总计---------------------------------------------');
  totalPrice.value = totalPrice.value.toFixed(2)
  nextTick(async ()=>{
	  uni.hideLoading()
	  loading.value = true
  })
}
onLoad(async(e)=>{
  endTime.value=e.time
	cartList.value=storeCart.cart
  orderList.value=e.orderList
	requestObj.value=JSON.parse(e.request)
	uni.showLoading({
		title: '计算中',
		mask: false
	})
	await sumFn()
})


// 用户备注赋值
const blurFn=(e)=>{
  if (e.detail.value.length > 20) {
  uni.showToast({
    title: '输入内容已达到最大长度',
    icon: 'none'
  });
}
  const storeCart=useCartStore()
  notes.value=e.detail.value
	storeCart.cartObj.notes= notes.value

}
// 点击确认跳转待支付页面
const onConfirm=async()=>{
  // 总金额和后端返回数据金额一致，则跳转页面
	// const storeCart=useCartStore()
	// loading.value=true
	// if(storeCart.cartObj.totalMoney==sum.value){
	//   loading.value=false
	// }
	// if(sum.value>0){
		if(loading.value){
			uni.navigateTo({url: `/pages/order_pay/order_pay?payobj=${JSON.stringify(requestObj.value)}&sum=${sum.value}&way=${way.value}&endTime=${endTime.value}&orderList=${orderList.value}`})
		} else {
			uni.showToast({title:'已完成支付',icon:'error',duration:500})
		}
	// } else {
	// 	uni.showToast({title:'余额不足，请前往收费处缴费！',icon:'none',duration:2000})
	// }
	
}
// const onConfirmFn=debounce(onConfirm,500)//防抖
</script>

<style scoped lang="scss">
.kk {
  padding-bottom: calc(80rpx + env(safe-area-inset-bottom) / 2);
}

.profile {
  height: 130rpx;
  padding: 40rpx 0;
  border-radius: 0 0 40rpx 50rpx;
  background-color: #0081ff;

}

.bg-set {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: #f4f4f4;
  z-index: -1;
}

.address,
.address_1 {

  display: flex;
  align-items: center;

}

.address_1 {
  justify-content: space-between;
  padding: 0 30rpx;
  box-sizing: border-box;
}

.mode {
  width: 100rpx;
  height: 50rpx;
  border-radius: 12rpx;
  margin-right: 20rpx;
  text-align: center;
  line-height: 50rpx;
  background-color: #cce6ff;
  color: #0081ff;
}

.food {
  display: flex;
  width: 100%;
  justify-content: space-between;
  align-items: center;
  color: black;
  margin-bottom: 10rpx;

  .left {
    display: flex;
    align-items: center;

    .img {
      width: 150rpx;
      height: 150rpx;
      margin-right: 10rpx;
    }
  }
}

.btn {
  position: fixed;
  bottom: 0;
  left: 0;
  padding: 0;
  width: 100%;
  border-radius: 0;
 // height: 80rpx;
  line-height:calc(80rpx + env(safe-area-inset-bottom) / 2);
  text-align: center;
  background-color: #0081ff;
  color: #fff;
  height: calc(80rpx + env(safe-area-inset-bottom) / 2);
	padding-bottom: calc(env(safe-area-inset-bottom) / 2);
  box-shadow: -5rpx -4rpx 5px 0rpx rgba(0,0,0,0.5)  ;
}</style>


{

    "foodorderlist": [
      {
        "orderNo": "string",
        "orderTypeCode": "string",
        "restaurantSerials": "string",
        "restaurantName": "string",
        "orderDate": "string",
        "orderCategorySerials": "string",
        "orderCategoryName": "string",
        "totalAmount": "string",
        "addressSerials": "string",
        "addressId": "string",
        "addressLongName": "string",
        "addressLevel": "string",
        "customerId": "string",
        "customerName": "string",
        "phoneNumber": "string",
        "notes": "string",
        "foodpackagelist": [
          {
            "foodlist": [
              {
                
              }
            ]
          }
        ],
        "foodlist": [
          {
         
          }
        ]
      }
    ]
  
}